package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.widget.roundimageview.RoundedImageView;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.t4.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhiyicx on 2017/5/11.
 */

public class AdapterEventDetailMemberList extends RecyclerView.Adapter<MyViewHolder> {

    private List<ModelUser> members;
    private Context mContext;
    private UnitSociax mUnitSociax;

    public AdapterEventDetailMemberList(Context context, List<ModelUser> members) {
        this.mContext = context;
        this.members = members;
        if (members == null) {
            this.members = new ArrayList<>();
        }
        mUnitSociax = new UnitSociax(context);
    }

    public void refreshAll(List<ModelUser> members) {
        this.members.clear();
        this.members.addAll(members);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FrameLayout layout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.item_member_face, null);
        return new MyViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ModelUser user = members.get(holder.getLayoutPosition());
        Glide.with(mContext)
                .load(user.getFace())
                .asBitmap()
                .placeholder(R.drawable.default_user)
                .error(R.drawable.default_user)
                .into(new BitmapImageViewTarget(holder.imageView) {
                    @Override
                    public void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        holder.imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.logE(members.get(holder.getLayoutPosition()).getUid() + " ");
                Intent intent = new Intent(mContext, ActivityUserInfo_2.class);
                intent.putExtra("uid", members.get(holder.getLayoutPosition()).getUid());
                mContext.startActivity(intent);
            }
        });
        /*增加显示认证图标 8.2.2017*/
        holder.llUserGroup.removeAllViews();
        if (user.getUserApprove() != null && user.getUserApprove().getApprove() != null && !user.getUserApprove().getApprove().isEmpty()){
            mUnitSociax.addUserGroup(user.getUserApprove().getApprove(), holder.llUserGroup);
        }
    }

    @Override
    public int getItemCount() {
        return members.size();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {

    RoundedImageView imageView;
    LinearLayout llUserGroup; // 增加认证图标显示 8.2.2017

    public MyViewHolder(View itemView) {
        super(itemView);
        imageView = (RoundedImageView) itemView.findViewById(R.id.iv_member_user_head);
        llUserGroup = (LinearLayout) itemView.findViewById(R.id.ll_user_group);
    }
}